package com.yft.controller.posinformation;

import com.yft.entity.base.Order;
import com.yft.entity.base.OrderSort;
import com.yft.entity.base.Pager;
import com.yft.entity.base.PagerImpl;
import com.yft.entity.posinformation.PosBusinessType;
import com.yft.entity.posinformation.PosModel;
import com.yft.entity.posinformation.PosSupplyChannel;
import com.yft.enumeration.PosPattern;
import com.yft.service.pos_information.PosModelService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Controller
public class PosModelController {
	private static final String controllerPath = "/yft/posModel/";
//	private String viewpath = "/admin/base/jsp/PosModelManage/";
	private String jsonViewpath = "/admin/pos_information/jsp/json/";
	
	@Autowired(required = true)
	private PosModelService posModelService;

	private boolean checkNullBlank(Object[] array){
		if(array==null || array.length==0){
			return false;
		}
		for(int i=0;i<array.length;i++){
			if(array[i] != null && !"".equals(((String)array[i]).trim())){
				continue;
			}else{
				return false;
			}
		}
		return true;
	}
	@RequestMapping(controllerPath + "getPosModelList.html")
	public String getAgentInfoListJson(HttpServletRequest request,HttpServletResponse response, ModelMap model) {
		PosModel condition = new PosModel();
		Pager pager = new PagerImpl();
		String name = StringUtils.trimToEmpty(request.getParameter("name"));
		String limit = request.getParameter("limit");
		if(name!=null && name.length()>0){
			condition.setSe_modelName(name+"%");
		}
		int pageSize = 7;
		if (limit != null && limit.length() > 0)
			pageSize = NumberUtils.toInt(limit) > 0 ? NumberUtils.toInt(limit) : pageSize;
		String start = request.getParameter("start");
		int page = 1;
		if (start != null && start.length() > 0)
			page = NumberUtils.toInt(start) / pageSize + 1;
		pager.setPageSize(pageSize);
		pager.setPage(page);
		Order order = new Order();
		order.addOrder("createTime", OrderSort.DESC);
		
		List<PosModel> posModelList =  posModelService.getPosModelList(condition, order, pager);
		
		model.addAttribute("totalProperty",pager.getRecordCount());
		model.addAttribute("posModelList",posModelList);
		
		return jsonViewpath + "pos_model";
	}
	@RequestMapping(controllerPath + "addPosModel.html")
	public String addPosModel(HttpServletRequest request,HttpServletResponse response, ModelMap model) throws Exception {
		String supplyChannel = StringUtils.trimToEmpty(request.getParameter("supplyChannel"));
		String supplyChannelId = StringUtils.trimToEmpty(request.getParameter("supplyChannelId"));
		String pattern = StringUtils.trimToEmpty(request.getParameter("pattern"));
		String modelName = StringUtils.trimToEmpty(request.getParameter("modelName"));
		boolean flag = false;
		String error = "";
		if( checkNullBlank(new Object[]{modelName,pattern,supplyChannel,supplyChannelId})){
            Integer supplyChannelIdInt = NumberUtils.toInt(supplyChannelId,-1);
            if(supplyChannelIdInt!=-1){
                PosModel posModel = new PosModel();
                posModel.setModelName(modelName);
                posModel.setPattern(PosPattern.valueOf(pattern));

                posModel.setSupplyChannelId(supplyChannelIdInt);
                posModel.setChannelName(supplyChannel);
                posModel.setPosSupplyChannel(new PosSupplyChannel(supplyChannelIdInt));
                if(posModelService.addPosModel(posModel)!=null){
                    flag = true;
                }else{
                    error = "添加失败";
                }
            }else{
                error = "参数转换错误";
            }
		}else{
			error = "请将信息补全";
		}
		model.addAttribute("success", flag);
		model.addAttribute("error", error);
		return "message/messageJson";
	}

    @RequestMapping(controllerPath + "getPosModelListForCom.html")
    public String getPosModelListForCom(HttpServletRequest request,HttpServletResponse response, ModelMap model) {
        PosModel condition = new PosModel();
        Pager pager = new PagerImpl();
        String query = StringUtils.trimToEmpty(request.getParameter("query"));
        String supplyChannelIdStr = StringUtils.trimToEmpty(request.getParameter("supplyChannelId"));
        if(supplyChannelIdStr!=null && supplyChannelIdStr.length()>0){
            condition.setSupplyChannelId(NumberUtils.toInt(supplyChannelIdStr,-1));
        }
        String limit = request.getParameter("limit");
        condition.setSe_modelName(query+"%");
        int pageSize = 7;
        if (limit != null && limit.length() > 0)
            pageSize = NumberUtils.toInt(limit) > 0 ? NumberUtils.toInt(limit) : pageSize;
        String start = request.getParameter("start");
        int page = 1;
        if (start != null && start.length() > 0)
            page = NumberUtils.toInt(start) / pageSize + 1;
        pager.setPageSize(pageSize);
        pager.setPage(page);
        Order order = new Order();
        order.addOrder("createTime", OrderSort.DESC);

        List<PosModel> posModelList =  posModelService.getPosModelList(condition, order, pager);

        model.addAttribute("totalProperty",pager.getRecordCount());
        model.addAttribute("posModelList",posModelList);

        return jsonViewpath + "pos_model";
    }

}
